Binary Search

PDF versionPDF version

#include<stdio.h>
#include<conio.h>

void main()
{
int arr[10]={1,2,4,7,8,9,12,34,57,97};
int mid,lower=0,upper=9,num,flag;

clrscr();
printf("Enter the no to be searched");
scanf("%d",&num);

for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(arr[mid]==num)
{
printf("the no is at pos %d in the array",mid);
flag=0;
break;
}
if(arr[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
{
printf("Element found in the array");

}
getch();

}